/**
 * 2016/08/17
 */

//树菜单
var curMenu = null,
	zTree_Menu = null;

//	树结构定义
var setting = {
	view: {
		showLine: false,
		showIcon: false,
		selectedMulti: false,
		dblClickExpand: false,
		addDiyDom: function(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch");
			var videoObj = $("#" + treeNode.tId + "_a");
			icoObj = $("#" + treeNode.tId + "_ico");
			switchObj.remove();
			icoObj.before(switchObj);

			if(treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
				switchObj.before(spaceStr);
			}
			if(!treeNode.children) {
				var editStr = "<span data-id='" + treeNode.id + "' class='videoPlay video-icon-start'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
				videoObj.append(editStr);
			}
		}
	},
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		beforeClick: function(treeId, treeNode) {
			if(treeNode.level == 0) {
				var zTree = $.fn.zTree.getZTreeObj("monitor-list");
				zTree.expandNode(treeNode);
				return false;
			}
			return true;
		}
	}
};

//点击视频按钮 右侧窗口展示视频
var videoPlay = {
	//	播放视频
	play: function(id) {
		var videoList = $(".video-play-list li");
		$.each(videoList, function(num, obj) {
			var videoObj = $(obj);
			var videoHtml = videoObj.html();
			var videoWidth = videoObj.width();
			var videoHeight = videoObj.height();
			if(videoHtml == "") {
				var src = 'video.html?width=' + videoWidth + '&height=' + videoHeight
				var iframe = $('<iframe width="100%" id="videoIframe_' + id + '" height="100%" src="' + src + '" scrolling="no"></iframe>');
				videoObj.append(iframe)
				videoObj.attr("id", "video_" + id);
				//				id = "videoPlyer_" + id;
				//				var videoDiv = "<div id='" + id + "'></div>"
				//				videoObj.html(videoDiv);
				//				videoPlay.player(id, null, videoWidth, videoHeight);
				return false;
			}
		});
		return false;
	},
	//	停止视频 （去除）
	stop: function(id) {
		var frame = $("#videoIframe_" + id)
		id = "#video_" + id;
		frame[0].contentWindow.document.write(''); //清空iframe的内容
		frame[0].contentWindow.close(); //避免iframe内存泄漏
		frame.attr("src", "about:blank");
		$(id).html("");
		$(id).attr("id", "");
		$(id).remove();
	},
	player: function(id, url, width, height) {
		//视频插件初始化
		jwplayer(id).setup({
			file: 'd:\\test.mp4',
			image: "js/jwplayer/player.jpg",
			width: width,
			height: height,
			autostart: true
		});
	}
}

var zNodes = [{
	id: 1,
	pId: 0,
	name: "实时监控",
	open: true
}, {
	id: 11,
	pId: 1,
	name: "摄像头1号"
}, {
	id: 12,
	pId: 1,
	name: "摄像头2号"
}, {
	id: 13,
	pId: 1,
	name: "摄像头3号"
}, {
	id: 14,
	pId: 1,
	name: "摄像头4号"
}, {
	id: 15,
	pId: 1,
	name: "摄像头5号"
}, {
	id: 2,
	pId: 0,
	name: "历史数据"
}, {
	id: 21,
	pId: 2,
	name: "摄像头1号"
}, {
	id: 211,
	pId: 21,
	name: "昨天"
}, {
	id: 212,
	pId: 21,
	name: "前天"
}, {
	id: 22,
	pId: 2,
	name: "摄像头2号"
}, {
	id: 23,
	pId: 2,
	name: "摄像头3号"
}, {
	id: 24,
	pId: 2,
	name: "摄像头4号"
}, {
	id: 25,
	pId: 2,
	name: "摄像头5号"
}];

$(document).ready(function() {
	//初始化视频树
	var treeObj = $("#monitor-list");
	$.fn.zTree.init(treeObj, setting, zNodes);
	zTree_Menu = $.fn.zTree.getZTreeObj("monitor-list");
	zTree_Menu.expandAll(true);
	//视频按钮事件
	$("#monitor-list").on("click", ".videoPlay", function() {
		var _ = $(this);
		if(_.hasClass("video-icon-start")) {
			//			遍历是否所有窗口是否有视频
			var isVideo = true;
			var videoList = $(".video-play-list li");
			$.each(videoList, function(num, obj) {
				var videoObj = $(obj);
				var videoHtml = videoObj.html();
				if(videoHtml == "") {
					isVideo = false;
				}
			});
			if(isVideo) {
				alert("视频窗口已满!");
				return false;
			}
			//显示停止按钮
			_.attr("class", "videoPlay video-icon-stop");
			//逻辑代码
			videoPlay.play(_.data("id"));
		} else if(_.hasClass("video-icon-stop")) {
			//显示视频按钮
			_.attr("class", "videoPlay video-icon-start");
			//逻辑代码
			videoPlay.stop(_.data("id"));
		}
		return false;
	});
	//	点击tree列响应事件
	$("#monitor-list").on("click", "a", function() {});

	// 视频监控的滚动条
	$('#monitor-list').mCustomScrollbar({
		scrollInertia: 200,
		mouseWheel: {
			scrollAmount: 400
		}
	});

	//视频搜索
	$('#search').bind('input propertychange', function() {  
		var treeObj = $.fn.zTree.getZTreeObj("monitor-list");  
		var value = $('#search').val();
		var showNode = null;
		var type = $('#type option:selected').val();
		nodeList = treeObj.getNodesByParamFuzzy('name', value);  
		nodes = treeObj.getNodes();
		$.each(nodes, function(num) {
			if(type != "") {
				if(nodes[num].name == type) {
					showNode = nodes[num];
				}
			}
			treeObj.hideNodes(nodes[num].children);
		});  
		if(showNode != null) {
			$.each(nodes, function(num) {
				treeObj.hideNode(nodes[num]);
			});  
			treeObj.showNode(showNode);
		}
		treeObj.showNodes(nodeList);  
	});
});
